<?php

namespace common\security;

use think\Request;

class XRequestId
{
    private static $key = '2zm38w4zkMy7q4zV2hFuWbJ11FKhVk03';

    public static function make()
    {

    }

    /**
     * @param Request $request
     * @return bool
     */
    public static function verify($request = null)
    {
        if ($request instanceof Request) {
            $request_id = $request->header('X-Request-id');
        } elseif (empty($request)) {
            $request_id = request()->header('X-Request-id');
        } else {
            $request_id = $request;
        }

        if (empty($request_id)) {
            return false;
        }

        [$string, $sign, $timestamp] = array_pad(explode('.', $request_id), 3, null);

        $encrypt = md5(self::$key . $string . $timestamp);

        if (empty($sign) || $encrypt != $sign) {
            return false;
        }

        return true;
    }
}